package ca.kooki.codesnippet.views;

import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import ca.kooki.codesnippet.Activator;
import ca.kooki.codesnippet.CodeSnippet;
import ca.kooki.codesnippet.CodeSnippetPreferencePage;
import ca.kooki.codesnippet.Tag;
import ca.kooki.codesnippet.core.ActionResponse;

public class TagEntryViewHandler implements KeyListener {

  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````

  public Button buttonAdd;
  public Text inputTitle;
  public Text inputKeywords;
  public Text inputCode;
  public Label labelMessage;
  public CodeSnippet codeSnippet;
  public Tag tagToUpdate;
  
  //```````````````````````
  // CONSTRUCTOR
  //```````````````````````
  
  public TagEntryViewHandler(Button buttonAdd, Text inputTitle, Text inputKeywords, Text inputCode, Label labelMessage)
  {
    this.buttonAdd = buttonAdd;
    this.inputCode = inputCode;
    this.inputKeywords = inputKeywords;
    this.inputTitle = inputTitle;
    this.labelMessage = labelMessage;
    codeSnippet = null;
    tagToUpdate = null;
    
    keyReleased(null);
  }
  
  //```````````````````````
  // INTERFACE
  //```````````````````````
  
  public void tagExistingCodeSnippet(Tag tag)
  {
    codeSnippet = tag.getCodeSnippet();
    tagToUpdate = null;
    updateInputs(tag);
  }
  
  public void updateTag(Tag tag)
  {
    codeSnippet = tag.getCodeSnippet();
    tagToUpdate = tag;
    updateInputs(tag);
  }

  
  public void keyPressed(KeyEvent e) {
  }

  public void keyReleased(KeyEvent e) {
    buttonAdd.setEnabled(canAddCodeSnippet());
    labelMessage.setText("");
  }
  
  public void addNewTag() 
  {
    
    String title = inputTitle.getText();
    String keywords = inputKeywords.getText();
    String code = inputCode.getText();
    
    ensureCodeSnippetUnchanged();
    
    CodeSnippet snippet = codeSnippet != null ? codeSnippet : new CodeSnippet(title,keywords,code);
    String username = Activator.getDefault().getPreferences().getString(CodeSnippetPreferencePage.USER_PREFERENCE);
    
    Tag tag = new Tag(title,keywords,snippet,username);
    
    if (tagToUpdate != null)
    {
      tag.setId(tagToUpdate.getId());
    }
    
    labelMessage.setText("Saving ...");
    ActionResponse response = Activator.getDefault().getRepository().addTag(tag);
    if (response.isValid())
    {
      codeSnippet = null;
      tagToUpdate = null;
      keyReleased(null);
    }
    labelMessage.setText(response.getMessage());
  }
    
  //```````````````````````
  // PRIVATE METHODS
  //```````````````````````
  
  private void ensureCodeSnippetUnchanged()
  {
    
    String newCode = inputCode.getText();
    
    if (codeSnippet != null && !codeSnippet.getCode().equals(newCode))
    {
      codeSnippet = null;
    }
  }
  
  private void updateInputs(Tag tag)
  {
    inputTitle.setText(tag.getTitle());
    inputKeywords.setText(tag.getKeywords());
    inputCode.setText(tag.getCodeSnippet().getCode());
    keyReleased(null);
  }

  
  private boolean canAddCodeSnippet()
  {
    String newText = null;

    newText = inputTitle.getText().trim();
    if (newText.equals(""))
    {
      return false;
    }

    newText = inputKeywords.getText().trim();
    if (newText.equals(""))
    {
      return false;
    }
    
    newText = inputCode.getText().trim();
    if (newText.equals(""))
    {
      return false;
    }
    
    return true;
  }
   
}
